<?php
namespace app\common\logic;

use think\facade\Db;

class Wallet
{
    /**
     * 获取钱包
     */
    public function getName($wallet_key=''){
        $ary = array('money'=>'现金');
        $value = tpCache('wallet.name');
        if($value){
            $ary =($value);
        }
        if($wallet_key){
           return $ary[$wallet_key];
        }
        return $ary;
    }

    /**
     * 获取会员所有余额总计
     */
    public function getUserSum(){
        $wallet = $this->getName();
        $ary = [];
        foreach($wallet as $key=>$val){
            $ary[$val] = Db::name('Wallet')->sum($key);
        }
        return $ary;
    }

    /**
     * 获取提现开关
     */
    public function getTx($filter=0){
        $ary = [];
        $value = tpCache('wallet.tx');
        if($value){
            $ary =($value);
        }
        //过滤不能提现的
        if($filter){
            foreach($ary as $key=>$val){
                if(!$val){
                    unset($ary[$key]);
                }
            }
        }
        return $ary;
    }

    /**
     * 获取充值开关
     */
    public function getCz($filter=0){
        $ary = [];
        $value = tpCache('wallet.cz');
        if($value){
            $ary =($value);
        }
        //过滤不能提现的
        if($filter){
            foreach($ary as $key=>$val){
                if(!$val){
                    unset($ary[$key]);
                }
            }
        }
        return $ary;
    }

    /**
     * 是否有充值
     */
    public function isCz(){
        $cz = $this->getCz();
        foreach($cz as $val){
            if($val){
                return true;
            }
        }
        return false;
    }

    /**
     * 是否有提现
     */
    public function isTx(){
        $cz = $this->getTx();
        foreach($cz as $val){
            if($val){
                return true;
            }
        }
        return false;
    }

    /**
     * 是否有转账
     */
    public function isZz(){
        $have = M('TransferSet')->where('status',1)->count();
        return $have;
    }

    /**
     * 获取在线支付方式
     */
    public function getOnlinePay($platform = 'wap'){
        $payment_where = [];
        $payment_where['status'] = 1;
        //手机端
//        if(isMobile()){
//            $payment_where['scene'] = array('in',array('0','1'));
//        }else{
//            $payment_where['scene'] = array('in',array('0','2'));
//        }
        $paymentList = Db::name('pay_way')->where($payment_where)->where('find_in_set("'.$platform.'",platform)')->select();
        if(!$paymentList){
            return ['status'=>0,'msg'=>'缺少在线支付方式'];
        }
        $paymentList = convert_arr_key($paymentList, 'type');

        $bankCodeList = [];
        foreach($paymentList as $key => $val)
        {
            //微信和支付宝
//            if(($key == 'alipayMobile' && is_weixin())){// || ($key == 'weixin' && !is_weixin())
//                unset($paymentList[$key]);
//                continue;
//            }
            //银行代码
            if($val['config_value']){
                $val['config_value'] =unserialize($val['config_value']);
//                if($val['config_value']['is_open']==0){
//                    unset($paymentList[$key]);
//                    continue;
//                }
                if($val['config_value']['is_bank'] == 1)
                {
                    $bankCodeList[$key] =($val['bank_code']);
                }
            }
        }

        //银行图片
        $bank_img = config('bank'); // 银行对应图片

        $result=[];
        $result['pay'] = $paymentList;
        $result['bank_img'] = $bank_img;
        $result['bankcode'] = $bankCodeList;
        return ['status'=>1,'result'=>$result];
    }

    /**
     * 获取所有在线支付
     */
    public function  getPay(){
        $payment_where['type'] = 'payment';
        $payment_where['status'] = 1;

        $paymentList = M('Plugin')->where($payment_where)->getField('code,name');
        if(!$paymentList){
            $paymentList = [];
        }
        return $paymentList;
    }

    //获取提现方式
    public function getTxType(){
        return [
            'wechat'=>'微信',
            'alipay'=>'支付宝',
            'bank'=>'银行卡',
//            'usdt'=>'USDT',
        ];
    }

    //奖金记录
    public function bonusDetail($user_info,$bonus_type='',$money_type=0,$bonus_money=0,$memo='',$data_table='',$data_id=0,$daishu=0,$order_userid=0,$order_username='',$is_send=0){
        $modtime = getCurTime();
        if(!in_array($bonus_type,['intro_bonus','chelv_bonus'])){
            $riqi = strtotime(date('Y-m-d',$modtime-86400));
        }else{
            $riqi = strtotime(date('Y-m-d',$modtime));
        }
        $w_date = date('Ymd',$riqi);
        //插入奖金记录
        $log = [];
        $log['username'] = $user_info['username'];
        $log['user_id'] = $user_info['user_id'];
        $log['level'] = $user_info['level'];
        $log['bonus_type'] = $bonus_type;
        $log['bonus'] = $bonus_money;
        $log['w_time'] = $modtime;
        $log['data_table'] = $data_table;
        $log['data_id'] = $data_id;
        $log['order_userid'] = $order_userid;
        $log['order_username'] = $order_username;
        $log['memo'] = $memo;
        $log['riqi'] = $riqi;
//        if($bonus_type=='fuli'){
//            $log['status'] = 1;
//        }else{
        $log['status'] = 0;
//        }
        $log['bili'] = 0;
        $send_bonus_7 = tpCache('basic.send_bonus_7');
        if($send_bonus_7){
            $log['ff_time'] = $riqi+7*86400;
        }else{
            $log['ff_time'] = $riqi+86400;
        }
        $log['w_date'] = $w_date;
        //dump($log);
        $log_id = Db::name('bonus_detail')->insertGetId($log);

        //累加会员奖金
        Db::name('users')->where('user_id',$user_info['user_id'])->setInc('lj_bonus',$bonus_money);

        //发放奖金
        //dump($bonus_money);
        if($is_send){
//            $res = logic('Bonus')->sendBonus($log_id);
//            if(!$res['status']){
//                return $res;
//            }
        }
        $res = ['status'=>true,'msg'=>'','log_id'=>$log_id];
        return $res;
    }
}